/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Lista.java
 *
 * Created on 22/04/2013, 05:57:54
 */
package gui;

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import visual.ontogrator.pkg2.Gene;

/**
 *
 * @author Gabriel
 */
public class ListaRede extends javax.swing.JFrame {
    
     DefaultListModel listSelecionado = new DefaultListModel();
     DefaultListModel listRede = new DefaultListModel();
     ArrayList<Gene> agenerede = new ArrayList<>();
     ArrayList<Gene> ageneselecionado = new ArrayList<>();

    public ArrayList<Gene> getAgeneselecionado() {
        return ageneselecionado;
    }

    public void setAgeneselecionado(ArrayList<Gene> ageneselecionado) {
        this.ageneselecionado = ageneselecionado;
        geraListaSelecionado();
        lstRede.setModel(listSelecionado);
        lstRede.repaint();
    }

    public ArrayList<Gene> getAgenerede() {
        return agenerede;
    }

    public void setAgenerede(ArrayList<Gene> agenerede) {
        this.agenerede = agenerede;
        geraListaRede();
        lstGenes.setModel(listRede);
        lstGenes.repaint();
        
    
    }


      
    /** Creates new form Lista */
    public ListaRede() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnRemoveGene = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstGenes = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        btnRemovePesquisa = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstRede = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        btnTransfere = new javax.swing.JButton();
        btnRemoveGene1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Network");
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 303));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnRemoveGene.setText("Remover");
        btnRemoveGene.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveGeneActionPerformed(evt);
            }
        });
        jPanel1.add(btnRemoveGene, new org.netbeans.lib.awtextra.AbsoluteConstraints(91, 287, -1, -1));

        jScrollPane3.setViewportView(lstGenes);

        jPanel1.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 125, 250));

        jLabel1.setText("Lista dos genes pesquisados:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        btnRemovePesquisa.setText("Remover");
        btnRemovePesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemovePesquisaActionPerformed(evt);
            }
        });
        jPanel1.add(btnRemovePesquisa, new org.netbeans.lib.awtextra.AbsoluteConstraints(286, 287, -1, -1));

        jScrollPane4.setViewportView(lstRede);

        jPanel1.add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(262, 31, 125, 250));

        jLabel3.setText("Lista de genes da rede:");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 10, -1, -1));

        btnTransfere.setText(">");
        btnTransfere.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTransfereActionPerformed(evt);
            }
        });
        jPanel1.add(btnTransfere, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 30, 71, -1));

        btnRemoveGene1.setText("Atualizar");
        btnRemoveGene1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveGene1ActionPerformed(evt);
            }
        });
        jPanel1.add(btnRemoveGene1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 287, -1, -1));

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnRemoveGeneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveGeneActionPerformed
int[] selecionados = lstGenes.getSelectedIndices();


       int size = selecionados.length-1;  
                  
                for(int i=size;i>=0;i--)  
                {  
                   agenerede.remove(selecionados[i]);  
                }  
                
           geraListaRede();
           lstGenes.setModel(listRede);
           lstGenes.repaint();
}//GEN-LAST:event_btnRemoveGeneActionPerformed

private void btnRemovePesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemovePesquisaActionPerformed
int[] selecionados = lstRede.getSelectedIndices();

       
       int size = selecionados.length-1;  
                  
                for(int i=size;i>=0;i--)  
                {  
                    
                    ageneselecionado.remove(selecionados[i]);  
                }  
                
           geraListaSelecionado();
           lstRede.setModel(listSelecionado);
           lstRede.repaint();
}//GEN-LAST:event_btnRemovePesquisaActionPerformed

private void transferir(int[] selecionados){
    int size = selecionados.length - 1;
    boolean flag = false;
    for (int i = size; i >= 0; i--) {
        Gene geneselecionado = agenerede.get(selecionados[i]);
        String selecionado = geneselecionado.getNome_Lista();
        for (int j = 0; j < ageneselecionado.size(); j++) {
            flag = false;
            Gene genelista = ageneselecionado.get(j);
            String lista = genelista.getNome_Lista();

            if (selecionado.equals(lista)) {
                flag = true;
                j=ageneselecionado.size();
            }


        }
        if (!flag) {
            ageneselecionado.add(geneselecionado);
        }

    }

           geraListaSelecionado();
           lstRede.setModel(listSelecionado);
           lstRede.repaint();
           
          
}

private void btnTransfereActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTransfereActionPerformed
int[] selecionados = lstGenes.getSelectedIndices();
      
transferir(selecionados);

    
}//GEN-LAST:event_btnTransfereActionPerformed

private void btnRemoveGene1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveGene1ActionPerformed
    
   //Atualizar itens selecionados com os novos
    ArrayList<Integer> aux = new ArrayList<>();
    int[] selecionados;
    ageneselecionado.clear();
    for (int i = 0; i < listSelecionado.size(); i++) {
            
        if(listRede.contains(listSelecionado.get(i))){
            aux.add(listRede.indexOf(listSelecionado.get(i)));
        }
        
    }
    
    selecionados = new int[aux.size()];
    
    for (int i = 0; i < aux.size(); i++) {
        selecionados[i]=aux.get(i);
    }
    
    transferir(selecionados);
    geraListaRede();
           lstGenes.setModel(listRede);
           lstGenes.repaint();
}//GEN-LAST:event_btnRemoveGene1ActionPerformed
private void geraListaRede(){
    listRede.clear();
    for (int i = 0; i < agenerede.size(); i++) {
                listRede.addElement(agenerede.get(i).getNome_Lista());
              
    }
}

private void geraListaSelecionado(){
    listSelecionado.clear();
    for (int i = 0; i < ageneselecionado.size(); i++) {
                listSelecionado.addElement(ageneselecionado.get(i).getNome_Lista());
         
    }
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListaRede.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListaRede.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListaRede.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListaRede.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ListaRede().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRemoveGene;
    private javax.swing.JButton btnRemoveGene1;
    private javax.swing.JButton btnRemovePesquisa;
    private javax.swing.JButton btnTransfere;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JList lstGenes;
    private javax.swing.JList lstRede;
    // End of variables declaration//GEN-END:variables
}
